{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bd11a433-2e76-4b2a-9f9c-0ec3fadf1461",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pydantic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4070b9d1-5748-4c0b-ae0f-c52983f9c421",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "from pydantic import BaseModel, PositiveInt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c3c7ee58-1b45-4904-a959-6565b81c3eb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "class User(BaseModel):\n",
    "    id: int\n",
    "    name: str = 'John Doe'\n",
    "    signup_ts: datetime | None\n",
    "    tastes: dict[str, PositiveInt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4ff14801-6cd1-4c70-af33-888520fd6508",
   "metadata": {},
   "outputs": [],
   "source": [
    "external_data = {\n",
    "    'id': 123,\n",
    "    'signup_ts': '2019-06-01 12:22',  \n",
    "    'tastes': {\n",
    "        'wine': 9,\n",
    "        b'cheese': 7,  \n",
    "        'cabbage': '1',  \n",
    "    },\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8bf9d068-2cf2-482f-a2e6-f87ff14b5747",
   "metadata": {},
   "outputs": [],
   "source": [
    "user = User(**external_data) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6884855d-e95a-4443-bb11-d306b9a9b19d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "print(user.id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5b7c5e28-436f-491c-abb9-13556e981bab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "User(id=123, name='John Doe', signup_ts=datetime.datetime(2019, 6, 1, 12, 22), tastes={'wine': 9, 'cheese': 7, 'cabbage': 1})"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1195b6cb-cc19-4748-8b95-174b39f613ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__abstractmethods__',\n",
       " '__annotations__',\n",
       " '__class__',\n",
       " '__class_getitem__',\n",
       " '__class_vars__',\n",
       " '__copy__',\n",
       " '__deepcopy__',\n",
       " '__delattr__',\n",
       " '__dict__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__fields__',\n",
       " '__fields_set__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__get_pydantic_core_schema__',\n",
       " '__get_pydantic_json_schema__',\n",
       " '__getattr__',\n",
       " '__getattribute__',\n",
       " '__getstate__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__lt__',\n",
       " '__module__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__pretty__',\n",
       " '__private_attributes__',\n",
       " '__pydantic_complete__',\n",
       " '__pydantic_core_schema__',\n",
       " '__pydantic_custom_init__',\n",
       " '__pydantic_decorators__',\n",
       " '__pydantic_extra__',\n",
       " '__pydantic_fields_set__',\n",
       " '__pydantic_generic_metadata__',\n",
       " '__pydantic_init_subclass__',\n",
       " '__pydantic_parent_namespace__',\n",
       " '__pydantic_post_init__',\n",
       " '__pydantic_private__',\n",
       " '__pydantic_root_model__',\n",
       " '__pydantic_serializer__',\n",
       " '__pydantic_validator__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__repr_args__',\n",
       " '__repr_name__',\n",
       " '__repr_str__',\n",
       " '__rich_repr__',\n",
       " '__setattr__',\n",
       " '__setstate__',\n",
       " '__signature__',\n",
       " '__sizeof__',\n",
       " '__slots__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " '__weakref__',\n",
       " '_abc_impl',\n",
       " '_calculate_keys',\n",
       " '_check_frozen',\n",
       " '_copy_and_set_values',\n",
       " '_get_value',\n",
       " '_iter',\n",
       " 'construct',\n",
       " 'copy',\n",
       " 'dict',\n",
       " 'from_orm',\n",
       " 'id',\n",
       " 'json',\n",
       " 'model_computed_fields',\n",
       " 'model_config',\n",
       " 'model_construct',\n",
       " 'model_copy',\n",
       " 'model_dump',\n",
       " 'model_dump_json',\n",
       " 'model_extra',\n",
       " 'model_fields',\n",
       " 'model_fields_set',\n",
       " 'model_json_schema',\n",
       " 'model_parametrized_name',\n",
       " 'model_post_init',\n",
       " 'model_rebuild',\n",
       " 'model_validate',\n",
       " 'model_validate_json',\n",
       " 'model_validate_strings',\n",
       " 'name',\n",
       " 'parse_file',\n",
       " 'parse_obj',\n",
       " 'parse_raw',\n",
       " 'schema',\n",
       " 'schema_json',\n",
       " 'signup_ts',\n",
       " 'tastes',\n",
       " 'update_forward_refs',\n",
       " 'validate']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(user)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ff2bc47b-0430-4cd9-ad5c-13cf2417b6fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 123,\n",
       " 'name': 'John Doe',\n",
       " 'signup_ts': datetime.datetime(2019, 6, 1, 12, 22),\n",
       " 'tastes': {'wine': 9, 'cheese': 7, 'cabbage': 1}}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vars(user)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "33210166-ed0b-42fb-8fc5-514232a986c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mappingproxy({'__module__': '__main__',\n",
       "              '__annotations__': {'id': int,\n",
       "               'name': str,\n",
       "               'signup_ts': datetime.datetime | None,\n",
       "               'tastes': dict[str, typing.Annotated[int, Gt(gt=0)]]},\n",
       "              'model_config': {},\n",
       "              '__class_vars__': set(),\n",
       "              '__private_attributes__': {},\n",
       "              '__weakref__': <attribute '__weakref__' of 'User' objects>,\n",
       "              '__doc__': None,\n",
       "              '__abstractmethods__': frozenset(),\n",
       "              '_abc_impl': <_abc._abc_data at 0x1f1c5fc4bc0>,\n",
       "              '__pydantic_custom_init__': False,\n",
       "              '__pydantic_post_init__': None,\n",
       "              '__pydantic_decorators__': DecoratorInfos(validators={}, field_validators={}, root_validators={}, field_serializers={}, model_serializers={}, model_validators={}, computed_fields={}),\n",
       "              '__pydantic_generic_metadata__': {'origin': None,\n",
       "               'args': (),\n",
       "               'parameters': ()},\n",
       "              '__pydantic_complete__': True,\n",
       "              '__pydantic_parent_namespace__': {'__name__': '__main__',\n",
       "               '__doc__': 'Automatically created module for IPython interactive environment',\n",
       "               '__package__': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c5062450>,\n",
       "               '__loader__': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c4e3e060>,\n",
       "               '__spec__': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c4f883e0>,\n",
       "               '__builtin__': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c4f898e0>,\n",
       "               '__builtins__': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c4f890a0>,\n",
       "               '_ih': ['',\n",
       "                'import pydantic',\n",
       "                'from datetime import datetime\\nfrom pydantic import BaseModel, PositiveInt',\n",
       "                \"class User(BaseModel):\\n    id: int\\n    name: str = 'John Doe'\\n    signup_ts: datetime | None\\n    tastes: dict[str, PositiveInt]\",\n",
       "                \"external_data = {\\n    'id': 123,\\n    'signup_ts': '2019-06-01 12:22',  \\n    'tastes': {\\n        'wine': 9,\\n        b'cheese': 7,  \\n        'cabbage': '1',  \\n    },\\n}\",\n",
       "                'user = User(**external_data) ',\n",
       "                'print(user.id)',\n",
       "                'user',\n",
       "                'dir(user)',\n",
       "                'vars(user)',\n",
       "                'vars(User)'],\n",
       "               '_oh': {7: User(id=123, name='John Doe', signup_ts=datetime.datetime(2019, 6, 1, 12, 22), tastes={'wine': 9, 'cheese': 7, 'cabbage': 1}),\n",
       "                8: ['__abstractmethods__',\n",
       "                 '__annotations__',\n",
       "                 '__class__',\n",
       "                 '__class_getitem__',\n",
       "                 '__class_vars__',\n",
       "                 '__copy__',\n",
       "                 '__deepcopy__',\n",
       "                 '__delattr__',\n",
       "                 '__dict__',\n",
       "                 '__dir__',\n",
       "                 '__doc__',\n",
       "                 '__eq__',\n",
       "                 '__fields__',\n",
       "                 '__fields_set__',\n",
       "                 '__format__',\n",
       "                 '__ge__',\n",
       "                 '__get_pydantic_core_schema__',\n",
       "                 '__get_pydantic_json_schema__',\n",
       "                 '__getattr__',\n",
       "                 '__getattribute__',\n",
       "                 '__getstate__',\n",
       "                 '__gt__',\n",
       "                 '__hash__',\n",
       "                 '__init__',\n",
       "                 '__init_subclass__',\n",
       "                 '__iter__',\n",
       "                 '__le__',\n",
       "                 '__lt__',\n",
       "                 '__module__',\n",
       "                 '__ne__',\n",
       "                 '__new__',\n",
       "                 '__pretty__',\n",
       "                 '__private_attributes__',\n",
       "                 '__pydantic_complete__',\n",
       "                 '__pydantic_core_schema__',\n",
       "                 '__pydantic_custom_init__',\n",
       "                 '__pydantic_decorators__',\n",
       "                 '__pydantic_extra__',\n",
       "                 '__pydantic_fields_set__',\n",
       "                 '__pydantic_generic_metadata__',\n",
       "                 '__pydantic_init_subclass__',\n",
       "                 '__pydantic_parent_namespace__',\n",
       "                 '__pydantic_post_init__',\n",
       "                 '__pydantic_private__',\n",
       "                 '__pydantic_root_model__',\n",
       "                 '__pydantic_serializer__',\n",
       "                 '__pydantic_validator__',\n",
       "                 '__reduce__',\n",
       "                 '__reduce_ex__',\n",
       "                 '__repr__',\n",
       "                 '__repr_args__',\n",
       "                 '__repr_name__',\n",
       "                 '__repr_str__',\n",
       "                 '__rich_repr__',\n",
       "                 '__setattr__',\n",
       "                 '__setstate__',\n",
       "                 '__signature__',\n",
       "                 '__sizeof__',\n",
       "                 '__slots__',\n",
       "                 '__str__',\n",
       "                 '__subclasshook__',\n",
       "                 '__weakref__',\n",
       "                 '_abc_impl',\n",
       "                 '_calculate_keys',\n",
       "                 '_check_frozen',\n",
       "                 '_copy_and_set_values',\n",
       "                 '_get_value',\n",
       "                 '_iter',\n",
       "                 'construct',\n",
       "                 'copy',\n",
       "                 'dict',\n",
       "                 'from_orm',\n",
       "                 'id',\n",
       "                 'json',\n",
       "                 'model_computed_fields',\n",
       "                 'model_config',\n",
       "                 'model_construct',\n",
       "                 'model_copy',\n",
       "                 'model_dump',\n",
       "                 'model_dump_json',\n",
       "                 'model_extra',\n",
       "                 'model_fields',\n",
       "                 'model_fields_set',\n",
       "                 'model_json_schema',\n",
       "                 'model_parametrized_name',\n",
       "                 'model_post_init',\n",
       "                 'model_rebuild',\n",
       "                 'model_validate',\n",
       "                 'model_validate_json',\n",
       "                 'model_validate_strings',\n",
       "                 'name',\n",
       "                 'parse_file',\n",
       "                 'parse_obj',\n",
       "                 'parse_raw',\n",
       "                 'schema',\n",
       "                 'schema_json',\n",
       "                 'signup_ts',\n",
       "                 'tastes',\n",
       "                 'update_forward_refs',\n",
       "                 'validate'],\n",
       "                9: {'id': 123,\n",
       "                 'name': 'John Doe',\n",
       "                 'signup_ts': datetime.datetime(2019, 6, 1, 12, 22),\n",
       "                 'tastes': {'wine': 9, 'cheese': 7, 'cabbage': 1}}},\n",
       "               '_dh': [WindowsPath('E:/Code/practice_makes_perfect/python')],\n",
       "               'In': ['',\n",
       "                'import pydantic',\n",
       "                'from datetime import datetime\\nfrom pydantic import BaseModel, PositiveInt',\n",
       "                \"class User(BaseModel):\\n    id: int\\n    name: str = 'John Doe'\\n    signup_ts: datetime | None\\n    tastes: dict[str, PositiveInt]\",\n",
       "                \"external_data = {\\n    'id': 123,\\n    'signup_ts': '2019-06-01 12:22',  \\n    'tastes': {\\n        'wine': 9,\\n        b'cheese': 7,  \\n        'cabbage': '1',  \\n    },\\n}\",\n",
       "                'user = User(**external_data) ',\n",
       "                'print(user.id)',\n",
       "                'user',\n",
       "                'dir(user)',\n",
       "                'vars(user)',\n",
       "                'vars(User)'],\n",
       "               'Out': {7: User(id=123, name='John Doe', signup_ts=datetime.datetime(2019, 6, 1, 12, 22), tastes={'wine': 9, 'cheese': 7, 'cabbage': 1}),\n",
       "                8: ['__abstractmethods__',\n",
       "                 '__annotations__',\n",
       "                 '__class__',\n",
       "                 '__class_getitem__',\n",
       "                 '__class_vars__',\n",
       "                 '__copy__',\n",
       "                 '__deepcopy__',\n",
       "                 '__delattr__',\n",
       "                 '__dict__',\n",
       "                 '__dir__',\n",
       "                 '__doc__',\n",
       "                 '__eq__',\n",
       "                 '__fields__',\n",
       "                 '__fields_set__',\n",
       "                 '__format__',\n",
       "                 '__ge__',\n",
       "                 '__get_pydantic_core_schema__',\n",
       "                 '__get_pydantic_json_schema__',\n",
       "                 '__getattr__',\n",
       "                 '__getattribute__',\n",
       "                 '__getstate__',\n",
       "                 '__gt__',\n",
       "                 '__hash__',\n",
       "                 '__init__',\n",
       "                 '__init_subclass__',\n",
       "                 '__iter__',\n",
       "                 '__le__',\n",
       "                 '__lt__',\n",
       "                 '__module__',\n",
       "                 '__ne__',\n",
       "                 '__new__',\n",
       "                 '__pretty__',\n",
       "                 '__private_attributes__',\n",
       "                 '__pydantic_complete__',\n",
       "                 '__pydantic_core_schema__',\n",
       "                 '__pydantic_custom_init__',\n",
       "                 '__pydantic_decorators__',\n",
       "                 '__pydantic_extra__',\n",
       "                 '__pydantic_fields_set__',\n",
       "                 '__pydantic_generic_metadata__',\n",
       "                 '__pydantic_init_subclass__',\n",
       "                 '__pydantic_parent_namespace__',\n",
       "                 '__pydantic_post_init__',\n",
       "                 '__pydantic_private__',\n",
       "                 '__pydantic_root_model__',\n",
       "                 '__pydantic_serializer__',\n",
       "                 '__pydantic_validator__',\n",
       "                 '__reduce__',\n",
       "                 '__reduce_ex__',\n",
       "                 '__repr__',\n",
       "                 '__repr_args__',\n",
       "                 '__repr_name__',\n",
       "                 '__repr_str__',\n",
       "                 '__rich_repr__',\n",
       "                 '__setattr__',\n",
       "                 '__setstate__',\n",
       "                 '__signature__',\n",
       "                 '__sizeof__',\n",
       "                 '__slots__',\n",
       "                 '__str__',\n",
       "                 '__subclasshook__',\n",
       "                 '__weakref__',\n",
       "                 '_abc_impl',\n",
       "                 '_calculate_keys',\n",
       "                 '_check_frozen',\n",
       "                 '_copy_and_set_values',\n",
       "                 '_get_value',\n",
       "                 '_iter',\n",
       "                 'construct',\n",
       "                 'copy',\n",
       "                 'dict',\n",
       "                 'from_orm',\n",
       "                 'id',\n",
       "                 'json',\n",
       "                 'model_computed_fields',\n",
       "                 'model_config',\n",
       "                 'model_construct',\n",
       "                 'model_copy',\n",
       "                 'model_dump',\n",
       "                 'model_dump_json',\n",
       "                 'model_extra',\n",
       "                 'model_fields',\n",
       "                 'model_fields_set',\n",
       "                 'model_json_schema',\n",
       "                 'model_parametrized_name',\n",
       "                 'model_post_init',\n",
       "                 'model_rebuild',\n",
       "                 'model_validate',\n",
       "                 'model_validate_json',\n",
       "                 'model_validate_strings',\n",
       "                 'name',\n",
       "                 'parse_file',\n",
       "                 'parse_obj',\n",
       "                 'parse_raw',\n",
       "                 'schema',\n",
       "                 'schema_json',\n",
       "                 'signup_ts',\n",
       "                 'tastes',\n",
       "                 'update_forward_refs',\n",
       "                 'validate'],\n",
       "                9: {'id': 123,\n",
       "                 'name': 'John Doe',\n",
       "                 'signup_ts': datetime.datetime(2019, 6, 1, 12, 22),\n",
       "                 'tastes': {'wine': 9, 'cheese': 7, 'cabbage': 1}}},\n",
       "               'get_ipython': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c4f88ad0>,\n",
       "               'exit': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c4f88350>,\n",
       "               'quit': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c4f88260>,\n",
       "               'open': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c5927230>,\n",
       "               '_': '',\n",
       "               '__': '',\n",
       "               '___': '',\n",
       "               '__session__': 'E:\\\\Code\\\\practice_makes_perfect\\\\python\\\\Untitled.ipynb',\n",
       "               '_i': 'from datetime import datetime\\nfrom pydantic import BaseModel, PositiveInt',\n",
       "               '_ii': 'import pydantic',\n",
       "               '_iii': '',\n",
       "               '_i1': 'import pydantic',\n",
       "               'pydantic': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c5926cf0>,\n",
       "               '_i2': 'from datetime import datetime\\nfrom pydantic import BaseModel, PositiveInt',\n",
       "               'datetime': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c5926db0>,\n",
       "               'BaseModel': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c5927080>,\n",
       "               'PositiveInt': <pydantic._internal._model_construction._PydanticWeakRef at 0x1f1c5926fc0>,\n",
       "               '_i3': \"class User(BaseModel):\\n    id: int\\n    name: str = 'John Doe'\\n    signup_ts: datetime | None\\n    tastes: dict[str, PositiveInt]\"},\n",
       "              'model_fields': {'id': FieldInfo(annotation=int, required=True),\n",
       "               'name': FieldInfo(annotation=str, required=False, default='John Doe'),\n",
       "               'signup_ts': FieldInfo(annotation=Union[datetime, NoneType], required=True),\n",
       "               'tastes': FieldInfo(annotation=dict[str, Annotated[int, Gt]], required=True)},\n",
       "              '__pydantic_core_schema__': {'type': 'model',\n",
       "               'cls': __main__.User,\n",
       "               'schema': {'type': 'model-fields',\n",
       "                'fields': {'id': {'type': 'model-field',\n",
       "                  'schema': {'type': 'int'},\n",
       "                  'metadata': {'pydantic_js_functions': [],\n",
       "                   'pydantic_js_annotation_functions': [<function pydantic._internal._generate_schema.get_json_schema_update_func.<locals>.json_schema_update_func(core_schema_or_field: 'CoreSchemaOrField', handler: 'GetJsonSchemaHandler') -> 'JsonSchemaValue'>]}},\n",
       "                 'name': {'type': 'model-field',\n",
       "                  'schema': {'type': 'default',\n",
       "                   'schema': {'type': 'str'},\n",
       "                   'default': 'John Doe'},\n",
       "                  'metadata': {'pydantic_js_functions': [],\n",
       "                   'pydantic_js_annotation_functions': [<function pydantic._internal._generate_schema.get_json_schema_update_func.<locals>.json_schema_update_func(core_schema_or_field: 'CoreSchemaOrField', handler: 'GetJsonSchemaHandler') -> 'JsonSchemaValue'>]}},\n",
       "                 'signup_ts': {'type': 'model-field',\n",
       "                  'schema': {'type': 'nullable',\n",
       "                   'schema': {'type': 'datetime',\n",
       "                    'microseconds_precision': 'truncate',\n",
       "                    'metadata': {'pydantic_js_annotation_functions': [<bound method InnerSchemaValidator.__get_pydantic_json_schema__ of InnerSchemaValidator(core_schema={'type': 'datetime', 'microseconds_precision': 'truncate', 'metadata': {'pydantic_js_annotation_functions': [<bound method InnerSchemaValidator.__get_pydantic_json_schema__ of ...>]}}, js_schema=None, js_core_schema=None, js_schema_update=None)>]}}},\n",
       "                  'metadata': {'pydantic_js_functions': [],\n",
       "                   'pydantic_js_annotation_functions': [<function pydantic._internal._generate_schema.get_json_schema_update_func.<locals>.json_schema_update_func(core_schema_or_field: 'CoreSchemaOrField', handler: 'GetJsonSchemaHandler') -> 'JsonSchemaValue'>]}},\n",
       "                 'tastes': {'type': 'model-field',\n",
       "                  'schema': {'type': 'dict',\n",
       "                   'keys_schema': {'type': 'str'},\n",
       "                   'values_schema': {'type': 'int', 'gt': 0},\n",
       "                   'strict': False},\n",
       "                  'metadata': {'pydantic_js_functions': [],\n",
       "                   'pydantic_js_annotation_functions': [<function pydantic._internal._generate_schema.get_json_schema_update_func.<locals>.json_schema_update_func(core_schema_or_field: 'CoreSchemaOrField', handler: 'GetJsonSchemaHandler') -> 'JsonSchemaValue'>]}}},\n",
       "                'model_name': 'User',\n",
       "                'computed_fields': []},\n",
       "               'custom_init': False,\n",
       "               'root_model': False,\n",
       "               'config': {'title': 'User'},\n",
       "               'ref': '__main__.User:2137884454416',\n",
       "               'metadata': {'pydantic_js_functions': [functools.partial(<function modify_model_json_schema at 0x000001F1C4DC8B80>, cls=<class '__main__.User'>, title=None),\n",
       "                 <bound method BaseModel.__get_pydantic_json_schema__ of <class '__main__.User'>>],\n",
       "                'pydantic_js_annotation_functions': []}},\n",
       "              '__pydantic_validator__': SchemaValidator(title=\"User\", validator=Model(\n",
       "                  ModelValidator {\n",
       "                      revalidate: Never,\n",
       "                      validator: ModelFields(\n",
       "                          ModelFieldsValidator {\n",
       "                              fields: [\n",
       "                                  Field {\n",
       "                                      name: \"id\",\n",
       "                                      lookup_key: Simple {\n",
       "                                          key: \"id\",\n",
       "                                          py_key: Py(\n",
       "                                              0x000001f1c4f88810,\n",
       "                                          ),\n",
       "                                          path: LookupPath(\n",
       "                                              [\n",
       "                                                  S(\n",
       "                                                      \"id\",\n",
       "                                                      Py(\n",
       "                                                          0x000001f1c4f89710,\n",
       "                                                      ),\n",
       "                                                  ),\n",
       "                                              ],\n",
       "                                          ),\n",
       "                                      },\n",
       "                                      name_py: Py(\n",
       "                                          0x00007ff9b6313418,\n",
       "                                      ),\n",
       "                                      validator: Int(\n",
       "                                          IntValidator {\n",
       "                                              strict: false,\n",
       "                                          },\n",
       "                                      ),\n",
       "                                      frozen: false,\n",
       "                                  },\n",
       "                                  Field {\n",
       "                                      name: \"name\",\n",
       "                                      lookup_key: Simple {\n",
       "                                          key: \"name\",\n",
       "                                          py_key: Py(\n",
       "                                              0x000001f1c5af3420,\n",
       "                                          ),\n",
       "                                          path: LookupPath(\n",
       "                                              [\n",
       "                                                  S(\n",
       "                                                      \"name\",\n",
       "                                                      Py(\n",
       "                                                          0x000001f1c5af1b60,\n",
       "                                                      ),\n",
       "                                                  ),\n",
       "                                              ],\n",
       "                                          ),\n",
       "                                      },\n",
       "                                      name_py: Py(\n",
       "                                          0x00007ff9b63146e0,\n",
       "                                      ),\n",
       "                                      validator: WithDefault(\n",
       "                                          WithDefaultValidator {\n",
       "                                              default: Default(\n",
       "                                                  Py(\n",
       "                                                      0x000001f1c5fc4c70,\n",
       "                                                  ),\n",
       "                                              ),\n",
       "                                              on_error: Raise,\n",
       "                                              validator: Str(\n",
       "                                                  StrValidator {\n",
       "                                                      strict: false,\n",
       "                                                      coerce_numbers_to_str: false,\n",
       "                                                  },\n",
       "                                              ),\n",
       "                                              validate_default: false,\n",
       "                                              copy_default: false,\n",
       "                                              name: \"default[str]\",\n",
       "                                              undefined: Py(\n",
       "                                                  0x000001f1c4a8d4c0,\n",
       "                                              ),\n",
       "                                          },\n",
       "                                      ),\n",
       "                                      frozen: false,\n",
       "                                  },\n",
       "                                  Field {\n",
       "                                      name: \"signup_ts\",\n",
       "                                      lookup_key: Simple {\n",
       "                                          key: \"signup_ts\",\n",
       "                                          py_key: Py(\n",
       "                                              0x000001f1c63183f0,\n",
       "                                          ),\n",
       "                                          path: LookupPath(\n",
       "                                              [\n",
       "                                                  S(\n",
       "                                                      \"signup_ts\",\n",
       "                                                      Py(\n",
       "                                                          0x000001f1c6318e30,\n",
       "                                                      ),\n",
       "                                                  ),\n",
       "                                              ],\n",
       "                                          ),\n",
       "                                      },\n",
       "                                      name_py: Py(\n",
       "                                          0x000001f1c5fc4a30,\n",
       "                                      ),\n",
       "                                      validator: Nullable(\n",
       "                                          NullableValidator {\n",
       "                                              validator: Datetime(\n",
       "                                                  DateTimeValidator {\n",
       "                                                      strict: false,\n",
       "                                                      constraints: None,\n",
       "                                                      microseconds_precision: Truncate,\n",
       "                                                  },\n",
       "                                              ),\n",
       "                                              name: \"nullable[datetime]\",\n",
       "                                          },\n",
       "                                      ),\n",
       "                                      frozen: false,\n",
       "                                  },\n",
       "                                  Field {\n",
       "                                      name: \"tastes\",\n",
       "                                      lookup_key: Simple {\n",
       "                                          key: \"tastes\",\n",
       "                                          py_key: Py(\n",
       "                                              0x000001f1c5af1b90,\n",
       "                                          ),\n",
       "                                          path: LookupPath(\n",
       "                                              [\n",
       "                                                  S(\n",
       "                                                      \"tastes\",\n",
       "                                                      Py(\n",
       "                                                          0x000001f1c5af1cb0,\n",
       "                                                      ),\n",
       "                                                  ),\n",
       "                                              ],\n",
       "                                          ),\n",
       "                                      },\n",
       "                                      name_py: Py(\n",
       "                                          0x000001f1c4f89920,\n",
       "                                      ),\n",
       "                                      validator: Dict(\n",
       "                                          DictValidator {\n",
       "                                              strict: false,\n",
       "                                              key_validator: Str(\n",
       "                                                  StrValidator {\n",
       "                                                      strict: false,\n",
       "                                                      coerce_numbers_to_str: false,\n",
       "                                                  },\n",
       "                                              ),\n",
       "                                              value_validator: ConstrainedInt(\n",
       "                                                  ConstrainedIntValidator {\n",
       "                                                      strict: false,\n",
       "                                                      multiple_of: None,\n",
       "                                                      le: None,\n",
       "                                                      lt: None,\n",
       "                                                      ge: None,\n",
       "                                                      gt: Some(\n",
       "                                                          I64(\n",
       "                                                              0,\n",
       "                                                          ),\n",
       "                                                      ),\n",
       "                                                  },\n",
       "                                              ),\n",
       "                                              min_length: None,\n",
       "                                              max_length: None,\n",
       "                                              name: \"dict[str,constrained-int]\",\n",
       "                                          },\n",
       "                                      ),\n",
       "                                      frozen: false,\n",
       "                                  },\n",
       "                              ],\n",
       "                              model_name: \"User\",\n",
       "                              extra_behavior: Ignore,\n",
       "                              extras_validator: None,\n",
       "                              strict: false,\n",
       "                              from_attributes: false,\n",
       "                              loc_by_alias: true,\n",
       "                          },\n",
       "                      ),\n",
       "                      class: Py(\n",
       "                          0x000001f1c3d7ee10,\n",
       "                      ),\n",
       "                      post_init: None,\n",
       "                      frozen: false,\n",
       "                      custom_init: false,\n",
       "                      root_model: false,\n",
       "                      undefined: Py(\n",
       "                          0x000001f1c4a8d4c0,\n",
       "                      ),\n",
       "                      name: \"User\",\n",
       "                  },\n",
       "              ), definitions=[], cache_strings=True),\n",
       "              '__pydantic_serializer__': SchemaSerializer(serializer=Model(\n",
       "                  ModelSerializer {\n",
       "                      class: Py(\n",
       "                          0x000001f1c3d7ee10,\n",
       "                      ),\n",
       "                      serializer: Fields(\n",
       "                          GeneralFieldsSerializer {\n",
       "                              fields: {\n",
       "                                  \"name\": SerField {\n",
       "                                      key_py: Py(\n",
       "                                          0x00007ff9b63146e0,\n",
       "                                      ),\n",
       "                                      alias: None,\n",
       "                                      alias_py: None,\n",
       "                                      serializer: Some(\n",
       "                                          WithDefault(\n",
       "                                              WithDefaultSerializer {\n",
       "                                                  default: Default(\n",
       "                                                      Py(\n",
       "                                                          0x000001f1c5fc4c70,\n",
       "                                                      ),\n",
       "                                                  ),\n",
       "                                                  serializer: Str(\n",
       "                                                      StrSerializer,\n",
       "                                                  ),\n",
       "                                              },\n",
       "                                          ),\n",
       "                                      ),\n",
       "                                      required: true,\n",
       "                                  },\n",
       "                                  \"signup_ts\": SerField {\n",
       "                                      key_py: Py(\n",
       "                                          0x000001f1c5fc4a30,\n",
       "                                      ),\n",
       "                                      alias: None,\n",
       "                                      alias_py: None,\n",
       "                                      serializer: Some(\n",
       "                                          Nullable(\n",
       "                                              NullableSerializer {\n",
       "                                                  serializer: Datetime(\n",
       "                                                      DatetimeSerializer,\n",
       "                                                  ),\n",
       "                                              },\n",
       "                                          ),\n",
       "                                      ),\n",
       "                                      required: true,\n",
       "                                  },\n",
       "                                  \"id\": SerField {\n",
       "                                      key_py: Py(\n",
       "                                          0x00007ff9b6313418,\n",
       "                                      ),\n",
       "                                      alias: None,\n",
       "                                      alias_py: None,\n",
       "                                      serializer: Some(\n",
       "                                          Int(\n",
       "                                              IntSerializer,\n",
       "                                          ),\n",
       "                                      ),\n",
       "                                      required: true,\n",
       "                                  },\n",
       "                                  \"tastes\": SerField {\n",
       "                                      key_py: Py(\n",
       "                                          0x000001f1c4f89920,\n",
       "                                      ),\n",
       "                                      alias: None,\n",
       "                                      alias_py: None,\n",
       "                                      serializer: Some(\n",
       "                                          Dict(\n",
       "                                              DictSerializer {\n",
       "                                                  key_serializer: Str(\n",
       "                                                      StrSerializer,\n",
       "                                                  ),\n",
       "                                                  value_serializer: Int(\n",
       "                                                      IntSerializer,\n",
       "                                                  ),\n",
       "                                                  filter: SchemaFilter {\n",
       "                                                      include: None,\n",
       "                                                      exclude: None,\n",
       "                                                  },\n",
       "                                                  name: \"dict[str, int]\",\n",
       "                                              },\n",
       "                                          ),\n",
       "                                      ),\n",
       "                                      required: true,\n",
       "                                  },\n",
       "                              },\n",
       "                              computed_fields: Some(\n",
       "                                  ComputedFields(\n",
       "                                      [],\n",
       "                                  ),\n",
       "                              ),\n",
       "                              mode: SimpleDict,\n",
       "                              extra_serializer: None,\n",
       "                              filter: SchemaFilter {\n",
       "                                  include: None,\n",
       "                                  exclude: None,\n",
       "                              },\n",
       "                              required_fields: 4,\n",
       "                          },\n",
       "                      ),\n",
       "                      has_extra: false,\n",
       "                      root_model: false,\n",
       "                      name: \"User\",\n",
       "                  },\n",
       "              ), definitions=[]),\n",
       "              '__signature__': <pydantic._internal._utils.ClassAttribute at 0x1f1c5af1b10>,\n",
       "              'model_computed_fields': {}})"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vars(User)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "522becaf-d1a0-4884-814a-55d734fe55e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "external_data = {'id': 'not an int', 'tastes': {}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ed18e5be-75ce-4c46-a023-9fc102bd47b1",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'ValidationError' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValidationError\u001b[0m                           Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m----> 2\u001b[0m     \u001b[43mUser\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mexternal_data\u001b[49m\u001b[43m)\u001b[49m  \n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m ValidationError \u001b[38;5;28;01mas\u001b[39;00m e:\n",
      "File \u001b[1;32mD:\\APPS\\conda\\Lib\\site-packages\\pydantic\\main.py:193\u001b[0m, in \u001b[0;36mBaseModel.__init__\u001b[1;34m(self, **data)\u001b[0m\n\u001b[0;32m    192\u001b[0m __tracebackhide__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m--> 193\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__pydantic_validator__\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalidate_python\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mself_instance\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mValidationError\u001b[0m: 2 validation errors for User\nid\n  Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='not an int', input_type=str]\n    For further information visit https://errors.pydantic.dev/2.8/v/int_parsing\nsignup_ts\n  Field required [type=missing, input_value={'id': 'not an int', 'tastes': {}}, input_type=dict]\n    For further information visit https://errors.pydantic.dev/2.8/v/missing",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m      2\u001b[0m     User(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mexternal_data)  \n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[43mValidationError\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m      4\u001b[0m     \u001b[38;5;28mprint\u001b[39m(e\u001b[38;5;241m.\u001b[39merrors())\n",
      "\u001b[1;31mNameError\u001b[0m: name 'ValidationError' is not defined"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    User(**external_data)  \n",
    "except ValidationError as e:\n",
    "    print(e.errors())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b02c77f1-a844-475c-8fda-084c57fc2136",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "when=datetime.datetime(2020, 1, 1, 12, 0) where=b'home'\n",
      "2 validation errors for Meeting\n",
      "when\n",
      "  Input should be a valid datetime [type=datetime_type, input_value='2020-01-01T12:00', input_type=str]\n",
      "    For further information visit https://errors.pydantic.dev/2.8/v/datetime_type\n",
      "where\n",
      "  Input should be a valid bytes [type=bytes_type, input_value='home', input_type=str]\n",
      "    For further information visit https://errors.pydantic.dev/2.8/v/bytes_type\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "from pydantic import BaseModel, ValidationError\n",
    "\n",
    "\n",
    "class Meeting(BaseModel):\n",
    "    when: datetime\n",
    "    where: bytes\n",
    "\n",
    "\n",
    "m = Meeting.model_validate({'when': '2020-01-01T12:00', 'where': 'home'})\n",
    "print(m)\n",
    "#> when=datetime.datetime(2020, 1, 1, 12, 0) where=b'home'\n",
    "try:\n",
    "    m = Meeting.model_validate(\n",
    "        {'when': '2020-01-01T12:00', 'where': 'home'}, strict=True\n",
    "    )\n",
    "except ValidationError as e:\n",
    "    print(e)\n",
    "    \"\"\"\n",
    "    2 validation errors for Meeting\n",
    "    when\n",
    "      Input should be a valid datetime [type=datetime_type, input_value='2020-01-01T12:00', input_type=str]\n",
    "    where\n",
    "      Input should be a valid bytes [type=bytes_type, input_value='home', input_type=str]\n",
    "    \"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "43770521-946e-419d-afa2-fe683ce44f1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "when=datetime.datetime(2020, 1, 1, 12, 0) where=b'home'\n"
     ]
    }
   ],
   "source": [
    "m_json = Meeting.model_validate_json(\n",
    "    '{\"when\": \"2020-01-01T12:00\", \"where\": \"home\"}'\n",
    ")\n",
    "print(m_json)\n",
    "#> when=datetime.datetime(2020, 1, 1, 12, 0) where=b'home'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1801d382-0cc1-4b3f-90a9-bec25ebbe079",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
